public class LongestOnes {

    public static void main(String[] args) {
        int[] nums = {1,1,1,0,0,0,1,1,1,1,0};
        LongestOnes longestOnes = new LongestOnes();
        System.out.println(longestOnes.longestOnes(nums, 2));
    }

    public int longestOnes(int[] nums, int k) {
        int l = 0;
        int count = 0;
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0) count++;
            while (count > k && l <= i) {
                if (nums[l] == 0) count--;
                l++;
            }
            ans = Math.max(ans, i - l + 1);
        }
        return ans;
    }
}
